package cn.edu.nju.oraldefense.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.edu.nju.oraldefense.dao.GraduateDao;
import cn.edu.nju.oraldefense.model.Graduate;
import cn.edu.nju.oraldefense.model.Teacher;

@Component("graduateDao")
public class GraduateDaoImpl implements GraduateDao {

	HibernateTemplate hibernateTemplate;
	
	private static final String NUMBER = "number";
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(Graduate instance) {
		hibernateTemplate.save(instance);
	}

	public int findCount() {
		String queryString = "select count(*) from Graduate";
		Long count = (Long)getHibernateTemplate().find(queryString)
				.listIterator().next();
		return count.intValue();
	}

	public int findCountByTeacher(Teacher teacher) {
		String queryString = "select count(*) from Graduate where teacher = ?";
		Long count = (Long)getHibernateTemplate().find(queryString,teacher)
				.listIterator().next();
		return count.intValue();
	}
	
	public List findAll() {
		String queryString = "from Graduate as model order by model.number asc";
		return getHibernateTemplate().find(queryString);
	}

	public List findByPage(final int start, final int limit) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session)
					throws HibernateException {
				Query query = session
						.createQuery("from Graduate as model order by model.number asc");
				query.setFirstResult(start);
				query.setMaxResults(limit);
				return query.list();
			}
		});
	}

	public List findByTeacherAndPage(final Teacher teacher,final int start,final int limit) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session)
					throws HibernateException {
				Query query = session
						.createQuery("from Graduate as model where model.teacher = ? order by model.number asc");
				query.setParameter(0, teacher);
				query.setFirstResult(start);
				query.setMaxResults(limit);
				return query.list();
			}
		});
	}
	
	public Graduate findById(int id) {
		Graduate instance = (Graduate) getHibernateTemplate().get(
				"cn.edu.nju.oraldefense.model.Graduate", id);
		return instance;
	}

	public List findByProperty(String propertyName, Object value) {
		String queryString = "from Graduate as model where model."
				+ propertyName + "=?";
		return hibernateTemplate.find(queryString, value);
	}

	public List findByNumber(String number) {
		return findByProperty(NUMBER, number);
	}
	
	public void update(Graduate instance) {
		hibernateTemplate.update(instance);
	}

	public void delete(Graduate instance) {
		hibernateTemplate.delete(instance);
	}
}
